<?php

define('ACC',true);

require ('../includes/init.php');
require ('left.php');
$category = new CategoryModel();

if(isset($_SESSION['user_id']) && isset($_SESSION['username'])){
	$smarty->assign('SESSION', $_SESSION);
}

if(isset($_GET['act']) && ($_GET['act'] == 'activity')){
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
	$smarty->assign('action', 'return');
	$smarty->assign('url', 'index.php');
	msg($smarty,'当前没有活动');
}

if(isset($_GET['cat_id']) && (($_GET['cat_id']+0) >= 0)){
	$cat_id = $_GET['cat_id'] + 0;
}else if(isset($_POST['cat_id']) && (($_POST['cat_id']+0) >= 0)){
	$cat_id = $_POST['cat_id'] + 0;
}else{
	echo '访问出错!';
	exit;
}

if($cat_id <= 10){
	$rowCount = $category->getRowCountGoods1($cat_id);
	$pageSize = 12;
}else{
	$rowCount = $category->getRowCountGoods2($cat_id);
	$pageSize = 12;
}

$category->setMessage($rowCount, $pageSize);
$pageCount = $category->getPageCount();

if($pageCount <= 5){
	$category->setMaxPageSize($pageCount);
}else{
	$category->setMaxPageSize(5);
}

if(isset($_POST['pageNow'])){
	$pageNow = $_POST['pageNow'] + 0;
	if($pageNow <= 0){
		$pageNow = 1;
	}else if($pageNow > $pageCount){
		$pageNow = $pageCount;
	}
}else{
	$pageNow = 1;
}

$category->setPageNow($pageNow);

$navigate = $category->getFenyeCode();

$smarty->assign('pageNow', $pageNow);
$smarty->assign('firstPage', 1);
$smarty->assign('lastPage', $pageCount);
$smarty->assign('pageCount', $pageCount);
$smarty->assign('navigate', $navigate);


if($cat_id < 10){

	if(isset($_POST['sort'])){

		$sort = trim($_POST['sort']);
		$sort_value = $sort;
		$sort = explode("+", $sort);
		if(!isset($sort[0])){
			$sort[0] = '';
		}else if(!isset($sort[1])){
			$sort[1] = '';
		}
		//print_r($sort);exit;
		$goods = $category->get_goods_sort($cat_id, $sort, $pageNow, $pageSize);
		//print_r($goods);exit;
		$smarty->assign('sort_value', $sort_value);
		$smarty->assign('sort', $sort);
	
	}else{
		
		$goods = $category->get_goods_info($cat_id, $pageNow, $pageSize);
		
	}
	$cat_name = $category->get_cat_name($cat_id);
	
	$smarty->assign('cat_id', $cat_id);
	$smarty->assign('action', 'parent_cat');
	$smarty->assign('goods', $goods);
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>' . $cat_name);
	$smarty->display('./front/goodsbox.html');
	
}else{

	if(isset($_GET['act']) && $_GET['act'] == 'sort'){
		$sort = trim($_POST['sort']);
		$sort_value = $sort;
		$sort = explode("+", $sort);
	}else if(!isset($sort[0])){
		$sort[0] = '';
	}else if(!isset($sort[1])){
		$sort[1] = '';
	}
		
	if(isset($_POST['cat_id'])){
		$arr = $_POST;
	
		if(($cat_id = ($_POST['cat_id'] + 0)) == 0){
			$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
			$smarty->assign('action', 'return');
			$smarty->assign('url', 'index.php');
			msg($smarty,'非法访问！');
		}
	
		$i = 0;
		$attrs_value = array();
		$selected = array();
		foreach($arr as $key => $val){
			if(is_numeric($key)){
				$attrs_value[]['attr_value'] = $val;
				$selected[$i]['attr_id'] = $key;
				$selected[$i]['attr_value'] = $val;
			}
			++$i;
		}
	
		if($cat_id >= 1 && $cat_id <= 10){
			$goods_list = $category->get_son_cat($cat_id);
		}else{
			$goods_list = $category->get_goods_list($cat_id, $attrs_value, $sort, $pageNow, $pageSize);
		}
	
		$cat_attrs = $category->get_cat_attrs($cat_id);
	
		foreach($cat_attrs as $key => $val){
			if($val['attr_input_type'] == '列表中选择'){
				$arr = explode(',', trim($val['attr_values']));
				$cat_attrs[$key]['attr_values'] = $arr;
	
				foreach($selected as $k => $v){
					if($val['attr_id'] == $v['attr_id']){
						$cat_attrs[$key]['selected'] = trim($v['attr_value']);
					}
				}
			}
		}
		$smarty->assign('sort_value', $sort_value);
		$smarty->assign('sort', $sort);
		$smarty->assign('cat_attrs', $cat_attrs);
		$smarty->assign('cat_id', $cat_attrs[0]['cat_id']);
		
	}else{
	
		$cat_attrs = $category->get_cat_attrs($cat_id);

		foreach($cat_attrs as $key => $val){
			if($val['attr_input_type'] == '列表中选择'){
				$arr = explode(',', $val['attr_values']);
				$cat_attrs[$key]['attr_values'] = $arr;
			}
		}
		$goods_list = $category->get_goods($cat_id, $pageNow, $pageSize);
		$smarty->assign('cat_attrs', $cat_attrs);
		$smarty->assign('cat_id', $cat_attrs[0]['cat_id']);
	}

	// ur_here
	$cat_name = $category->get_cat_name($cat_id);
	$parent = $category->get_parent($cat_id);
	$ur_here = '当前位置:&nbsp;<a href="index.php">首页&nbsp;</a><code>&gt;</code><a href="category.php?cat_id=' . $parent['cat_id'] . '">&nbsp;' . $parent['cat_name'] . '</a>&nbsp;&gt;&nbsp;' . $cat_name;
	$smarty->assign('ur_here', $ur_here);
	
	$smarty->assign('cat_id', $cat_id);
	$smarty->assign('goods_list', $goods_list);
	
	$smarty->display('./front/category.html');
	
	
}






?>
